package cn.wl.pchart.service.impl;

import cn.wl.pchart.dao.PchartMapper;
import cn.wl.pchart.dao.UserMapper;
import cn.wl.pchart.entity.Pcharts;
import cn.wl.pchart.entity.Pinfo;
import cn.wl.pchart.service.PchartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

// 纸飞机服务实现
@Service("PchartService")
public class PchartServiceImpl implements PchartService {

    @Autowired
    PchartMapper pchartMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public Pcharts getPchartByPid(String pid, String id) {
        if (pid == null) {
            return null;
        }
        Pcharts pcharts = new Pcharts();
        try {

            pcharts.setPchart(pchartMapper.getPchartByPid(pid));
            pcharts.setPinfos(pchartMapper.getPfinos(pid));

            // 获取评论者们的昵称和头像
            for (Pinfo i : pcharts.getPinfos())
            {
                i.setName(userMapper.getName(i.getId()));
                i.setImage(userMapper.getImage(i.getId()));
            }

            // 评论数量
            pcharts.setCommentNums(pcharts.getPinfos().size());

            // 设置纸飞机用户昵称和头像
            pcharts.setName(userMapper.getName(pcharts.getPchart().getId()));
            pcharts.setImage(userMapper.getImage(pcharts.getPchart().getId()));

            if (id != null)
            {
                // 设置用户是否关注此纸飞机作者
                pcharts.setLike(userMapper.isLike(id, pcharts.getPchart().getId()).equals(1));
                System.out.println("是否关注？" + userMapper.isLike(id, pcharts.getPchart().getId()));
            }

            return pcharts;
        } catch (Exception e)
        {
            return null;
        }
    }

    @Override
    public Pcharts getPchart(String id) {
        return getPchartByPid(pchartMapper.getPchart(), id);
    }

    @Override
    public Pcharts getPchartById(String id, String time) {
        Pcharts pcharts =  getPchartByPid(pchartMapper.getPchartsById(id, time), null);
        if (userMapper.isLike(id, pcharts.getPchart().getId()) == 1) {
            pcharts.setLike(true);
        } else {
            pcharts.setLike(false);
        }
        return pcharts;
    }

    @Override
    public List<Pcharts> getAllPchartById(String id) {
        List<String> pids = pchartMapper.getAllPchartsById(id);
        List<Pcharts> res = new ArrayList<Pcharts>();
        for (String i : pids)
        {
            Pcharts pcharts = getPchartByPid(i, null);
            if (userMapper.isLike(id, pcharts.getPchart().getId()) == 1) {
                pcharts.setLike(true);
            } else {
                pcharts.setLike(false);
            }
            res.add(pcharts);
        }
        return res;
    }

    @Override
    public void sendPchart(String id, String data) {
        pchartMapper.sendPchart(id, data);
    }

    @Override
    public void deletePchart(String pid) {
        pchartMapper.deletePchart(pid);
    }

    @Override
    public void commentPchart(String pid, String id, String data) {
        pchartMapper.commentPchart(pid, id, data);
    }
}
